<template>
  <div class="">
    <p-modal
      title="新增班组"
      :visible="visible"
      :confirm-loading="confirmLoading"
      @ok="handleOk"
      @cancel="handleCancel"
    >
      <p-form-model
        ref="ruleForm"
        :model="form"
        :rules="rules"
        :label-col="labelCol"
        :wrapper-col="wrapperCol"
      >
        <p-form-model-item ref="classno" label="班组编码" prop="classno">
          <p-input
            :disabled = "state == 'edit'"
            v-model="form.classno"
          />
        </p-form-model-item>
        <p-form-model-item ref="classname" label="班组名称" prop="classname">
          <p-input
            v-model="form.classname"
          />
        </p-form-model-item>

        <p-form-model-item ref="classdescription" label="班组描述" prop="classdescription">
          <p-input
            v-model="form.classdescription"
          />
        </p-form-model-item>
      </p-form-model>
    </p-modal>
  </div>
</template>

<script type="text/javascript">
import { postAction, getAction, deleteAction, putAction } from "@/api/manage";


export default {
  data() {
    return {
      labelCol: {
        span: 6
      },
      wrapperCol: {
        span: 12
      },
      visible: false,
      confirmLoading: false,
      form: {
        classno: '',
        classname: '',
        classdescription: ''
      },      
      rules: {
        classno: [
          { required: true, message: '请输入班组编码', trigger: 'blur' },
        ],
        classname: [
          { required: true, message: '请输入班组名称', trigger: 'blur' },
        ],
        classdescription: [
          { required: true, message: '请输入班组描述', trigger: 'blur' },
        ],
      },
      url: {
        add: '/mes-manpower/mesHrClassInfo',
        edit: '/mes-manpower/mesHrClassInfo', // 编辑

      },
      state: 'add'
    };
  },
  mounted() {},
  methods: {

    showModal(record) {
      this.visible = true;
      if(record) {
        this.state = 'edit'
        this.form.id = record.id
        this.form.classno = record.classno
        this.form.classname = record.classname
        this.form.classdescription = record.classdescription
      }
    },
    handleOk(e) {
      this.confirmLoading = true;

      this.$refs.ruleForm.validate(valid => {
        if (valid) {


          if(this.state == 'add' ) {

            const params = {
              ...this.form,
              parentId: '-1'
            }

            postAction(this.url.add, params).then(res => {
              if( res.success ) {
                this.visible = false
                this.$emit('handleSuccess')
              }else {
                this.$message.error(res.msg)
              }
              this.confirmLoading = false;
            })

          }

          if( this.state == 'edit' ){
            const params = {
              ...this.form,
              parentId: '-1'
            }            
            putAction(this.url.edit, params).then(res => {
              if( res.success ) {
                this.visible = false
                this.$emit('handleSuccess')
              }else {
                this.$message.error(res.msg)
              }
              this.confirmLoading = false;
            })
          }



        } else {
          this.confirmLoading = false;

          return false;
        }
      });


    },
    handleCancel(e) {
      console.log('Clicked cancel button');
      this.visible = false;
    },

  },
  components: {},
};
</script>

<style lang="less" scoped>
</style>
